//
//  PayFactory.m
//  ProjectPublic
//
//  Created by ac-hu on 2018/7/14.
//  Copyright © 2018年 ac hu. All rights reserved.
//

#import "PayFactory.h"
#import "PayAli.h"
#import "PayWx.h"
#import "PayUp.h"

@interface PayFactory()



@end

@implementation PayFactory

+(instancetype)share{
    static PayFactory *pay;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (pay == nil) {
            pay = [[PayFactory alloc]init];
        }
    });
    return pay;
}

+(id<PayStrategyProtocol>)getPayClass:(PayType)payType{
    [PayFactory share].type = payType;
    switch (payType) {
        case PayTypeWx:
            return [PayWx share];
            break;
        case PayTypeAli:
            return [PayAli share];
            break;
        case PayTypeUp:
            return [PayUp share];
            break;

        default:
            break;
    }
    return nil;
}

+(void)payOrder:(id)order succeed:(succeedPay)succeed fail:(failPay)fail{
    [[PayFactory getPayClass:[PayFactory share].type] payOrder:order succeed:succeed fail:fail];
}

//向服务器发起订单请求-支付开始
+(void)paySendNet:(id)order succeed:(succeedPay)succeed fail:(failPay)fail{
    NSDictionary *dic = @{@"type":@([PayFactory share].type),
                          @"orderNumber":[PayFactory share].orderNumber};
    [NetBaseRequest post:@"http:" dic:dic succeed:^(id result) {
        [PayFactory payOrder:result succeed:^(NSString *str) {
            [PayFactory payVerifyNetSucceed:succeed fail:fail];
        } fail:^(NSString *failStr) {
            fail(failStr);
        }];
    } fail:^(NSString *errorStr) {
        fail(errorStr);
    }];
}
//向服务器发起支付结果请求，交易是否成功
+(void)payVerifyNetSucceed:(succeedPay)succeed fail:(failPay)fail{
    NSDictionary *dic = @{@"type":@([PayFactory share].type),
                          @"orderNumber":[PayFactory share].orderNumber};
    [NetBaseRequest post:@"" dic:dic succeed:^(id result) {
        succeed(@"支付成功");
    } fail:^(NSString *errorStr) {
        fail(errorStr);
    }];
}

+(void)monitorResult:(NSURL *)url{
    [[PayFactory getPayClass:[PayFactory share].type] monitorResult:url];
}
@end

